using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class DryerItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			Up = 1,
			Launch = 2,
			Down = 3
		}

		public enum State
		{
			Down = 0,
			UpStatic = 1,
			UpLaunch = 2
		}

		private float DOWN_Y = -0.3f;

		private float UP_Y = 0.4f;

		private float NORMAL_ANGLE;

		private float LAUNCH_ANGLE = -20f;

		private float COVER_OPEN_X = -0.42f;

		private float COVER_CLOSE_X = 0.6f;

		private Transform dryersTrans;

		private Transform[] dryers;

		private Transform cover;

		private Anim animState;

		private float curCoverX;

		private float curY;

		private float dryerAngle;

		private int animStage;

		private State state;

		private Transform efcTrans;

		private Transform windEfc;

		private BaseItem carBrsuh;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			cover = model.Find("Cover");
			cover.SetLocalZ(1E-05f);
			dryersTrans = model.Find("Dryers");
			int childCount = dryersTrans.childCount;
			dryers = new Transform[childCount];
			for (int i = 0; i < childCount; i++)
			{
				dryers[i] = dryersTrans.GetChild(i);
				dryers[i].SetLocalZ((float)(2 * (i + 1)) * 1E-05f);
				dryers[i].GetChild(0).SetLocalZ(1E-05f);
			}
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			if (animState == Anim.Static)
			{
				return;
			}
			if (animState == Anim.Up)
			{
				if (animStage == 0)
				{
					curCoverX -= Time.deltaTime;
					if (curCoverX <= COVER_OPEN_X)
					{
						cover.SetLocalX(COVER_OPEN_X);
						animStage++;
					}
					else
					{
						cover.SetLocalX(curCoverX);
					}
				}
				else
				{
					curY += Time.deltaTime;
					if (curY >= UP_Y)
					{
						dryersTrans.SetLocalY(UP_Y);
						animState = Anim.Static;
					}
					else
					{
						dryersTrans.SetLocalY(curY);
					}
				}
			}
			else if (animState == Anim.Launch)
			{
				dryerAngle -= Time.deltaTime * 100f;
				if (dryerAngle <= LAUNCH_ANGLE)
				{
					SetDryersRotation(LAUNCH_ANGLE);
					animState = Anim.Static;
				}
				else
				{
					SetDryersRotation(dryerAngle);
				}
			}
			else
			{
				if (animState != Anim.Down)
				{
					return;
				}
				if (animStage == 0)
				{
					dryerAngle += Time.deltaTime * 100f;
					if (dryerAngle >= NORMAL_ANGLE)
					{
						SetDryersRotation(NORMAL_ANGLE);
						animStage++;
					}
					else
					{
						SetDryersRotation(dryerAngle);
					}
				}
				else if (animStage == 1)
				{
					curY -= Time.deltaTime;
					if (curY <= DOWN_Y)
					{
						dryersTrans.SetLocalY(DOWN_Y);
						animStage++;
					}
					else
					{
						dryersTrans.SetLocalY(curY);
					}
				}
				else
				{
					curCoverX += Time.deltaTime;
					if (curCoverX >= COVER_CLOSE_X)
					{
						cover.SetLocalX(COVER_CLOSE_X);
						animState = Anim.Static;
					}
					else
					{
						cover.SetLocalX(curCoverX);
					}
				}
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Down)
			{
				cover.SetLocalX(COVER_CLOSE_X);
				dryersTrans.SetLocalY(DOWN_Y);
				SetDryersRotation(NORMAL_ANGLE);
			}
			else if (state == State.UpStatic)
			{
				cover.SetLocalX(COVER_OPEN_X);
				dryersTrans.SetLocalY(UP_Y);
				SetDryersRotation(NORMAL_ANGLE);
			}
			else if (state == State.UpLaunch)
			{
				cover.SetLocalX(COVER_OPEN_X);
				dryersTrans.SetLocalY(UP_Y);
				SetDryersRotation(LAUNCH_ANGLE);
				PlayWindEffect();
			}
		}

		private void SetDryersRotation(float angle)
		{
			for (int i = 0; i < dryers.Length; i++)
			{
				dryers[i].GetChild(0).eulerAngles = new Vector3(0f, 0f, angle);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (animState == Anim.Static)
			{
				if (state == State.Down)
				{
					animState = Anim.Up;
					state = State.UpStatic;
					curY = DOWN_Y;
					animStage = 0;
					curCoverX = COVER_CLOSE_X;
					Singleton<AudioManager>.Instance.PlayAudio("sound_dryer_open", false, GetAudioPlayer());
				}
				else if (state == State.UpStatic)
				{
					state = State.UpLaunch;
					animState = Anim.Launch;
					dryerAngle = NORMAL_ANGLE;
					PlayWindEffect();
					Singleton<AudioManager>.Instance.PlayAudio("sound_car_dryer", true, GetAudioPlayer());
				}
				else if (state == State.UpLaunch)
				{
					animState = Anim.Down;
					state = State.Down;
					curY = UP_Y;
					animStage = 0;
					curCoverX = COVER_OPEN_X;
					dryerAngle = LAUNCH_ANGLE;
					StopWindEffect();
					Singleton<AudioManager>.Instance.PlayAudio("sound_dryer_close", false, GetAudioPlayer());
				}
			}
		}

		public override void UpdateFrontDepth()
		{
			base.UpdateFrontDepth();
			if (windEfc != null)
			{
				if (carBrsuh == null)
				{
					carBrsuh = Singleton<Scene>.Instance.GetItemByScriptType(18);
				}
				if (carBrsuh != null)
				{
					windEfc.position = new Vector3(efcTrans.position.x, efcTrans.position.y, carBrsuh.transform.position.z + 0.0004f);
				}
				else
				{
					windEfc.localPosition = new Vector3(0f, 0f, -0.0004f);
				}
			}
		}

		private void PlayWindEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.dragEfc))
			{
				return;
			}
			if (windEfc == null)
			{
				efcTrans = dryers[0].GetChild(0).Find("Efc");
				if (efcTrans != null)
				{
					efcTrans.SetLocalZ(-0.00024999998f);
					windEfc = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.dragEfc);
					if (windEfc != null)
					{
						windEfc.SetParent(efcTrans);
						carBrsuh = Singleton<Scene>.Instance.GetItemByScriptType(18);
						if (carBrsuh != null)
						{
							windEfc.position = new Vector3(efcTrans.position.x, efcTrans.position.y, carBrsuh.transform.position.z + 0.0004f);
						}
						else
						{
							windEfc.localPosition = new Vector3(0f, 0f, -0.0004f);
						}
						windEfc.localEulerAngles = new Vector3(0f, 0f, 0f);
						windEfc.localScale = Vector3.one;
					}
				}
			}
			SetEffectLoop(windEfc, true);
		}

		private void StopWindEffect()
		{
			SetEffectLoop(windEfc, false);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
